<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>React 组件基本了解</title>
</head>

<body>

    <div id="demo1"></div>
    <div id="demo2"></div>
    <div id="demo3"></div>
    <!-- 引入react核心库 -->
    <script type="text/javascript" src="../js/react.development.js"></script>
    <!-- 引入react-dom，用于支持react操作DOM -->
    <script type="text/javascript" src="../js/react-dom.development.js"></script>
    <!-- 引入babel，用于将jsx转为js -->
    <script type="text/javascript" src="../js/babel.min.js"></script>

    <script type="text/babel">
        //类组件
        class MyComponent2 extends React.Component {
            constructor(props) {
                super(props)
                const { defaule } = props
                this.state = {
                    isLike: defaule
                }
                // 将新方法中的this绑定为组件对象
                this.handlerClick = this.handlerClick.bind(this)
            }

            getIsLike() {
                return this.state.isLike
            }

            handlerClick() {
                // this.setState({
                //     isLike: !this.getIsLike()
                // })
                const isLike = !this.getIsLike();
                this.setState({
                    isLike
                })
            }

            //箭头函数的使用
            // handlerClick = (e) => {
            //     this.setState({
            //         isLike: !this.getIsLike()
            //     })
            // }

            // 重写组件的方法
            render() {
                const isLike = this.getIsLike()
                return (<h2 onClick={this.handlerClick}>
                    {isLike ? '晴天' : '阴天'}
                </h2>
                )
            }
        }

         //类组件
         class MyComponent extends React.Component {
            constructor(props) {
                super(props)
            }

          
            // 重写组件的方法
            render() {
                const {name,age,gender}=this.props
                return ( 
                    <div>
                        <ul>    
                            <li>{name}</li>
                            <li>{age}</li>
                            <li>{gender}</li>
                        </ul>
                    </div>
                )
            }
        }

        const Person={
            name:'张三',
            age:12,
            gender:'男'
        }
        ReactDOM.render(<MyComponent2 defaule={true} />, document.getElementById('demo2'))
        ReactDOM.render(<MyComponent {...Person} />, document.getElementById('demo1'))
    </script>
</body>

</html>